package com.benmei.weike.service;

import com.benmei.weike.common.filter.RequestPage;
import com.benmei.weike.dao.*;
import com.benmei.weike.dto.mcCourse.McCourseIntoClassRoomResponse;
import com.benmei.weike.dto.mcCourse.McCoursewareDto;
import com.benmei.weike.dto.teacher.McTeaScheduleDto;
import com.benmei.weike.dto.teacher.McTeacherCourseDto;
import com.benmei.weike.entity.McCourseSchedule;
import com.benmei.weike.entity.Teacher;
import com.benmei.weike.exception.ClientException;
import com.benmei.weike.exception.ServerException;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Peter on 2018/10/8.
 */
@Service
public class McCourseTeacherService {

    public static final Logger logger = LoggerFactory.getLogger(McCourseTeacherService.class);

    @Resource
    private McCourseDao mcCourseDao;
    @Resource
    private McAppointmentDao mcAppointmentDao;
    @Resource
    private McBuyerDao mcBuyerDao;
    @Resource
    private McGroupSettingDao mcGroupSettingDao;
    @Resource
    private McGroupInstanceDao mcGroupInstanceDao;
    @Resource
    private McCourseScheduleSignInDao mcCourseScheduleSignInDao;
    @Resource
    private McCourseScheduleDao mcCourseScheduleDao;
    @Resource
    private TeacherDao teacherDao;
    @Resource
    private StudentDao studentDao;


    // 进入教室
    public McCourseIntoClassRoomResponse intoClassRoom(Integer schedule_id, Integer tea_id) {
        McCourseIntoClassRoomResponse response = new McCourseIntoClassRoomResponse();

        McCourseSchedule schedule = mcCourseScheduleDao.findById(schedule_id);
        if (schedule == null) {
            throw new ClientException("教室不存在，请联系客服（schedule_id:" + schedule_id + "）");
        }
        // 查询老师信息
        Teacher teacher = teacherDao.getById(schedule.getTea_id());
        if (teacher == null) {
            logger.error("老师不存在，teacherId=" + schedule.getTea_id());
            throw new ServerException("进入教室失败：老师不存在，teacherId=" + schedule.getTea_id());
        }
        response.setTeacher_id(teacher.getTea_id());
        response.setTeacher_name(teacher.getTea_name());
        response.setTeacher_avatar(teacher.getTea_head_portrait());

        // 查询ppt
        List<McCoursewareDto> coursewareList = mcCourseDao.findCourseware(schedule_id);
        response.setCoursewareList(coursewareList);

        return response;
    }

    /**
     * 查询一对多老师课程
     * @param reqPage
     * @return
     */
    public List<McTeacherCourseDto> getMcCourses(RequestPage reqPage) {
        // 获取老师id
        Integer tea_id= (Integer) reqPage.getObj();
        //获取需求课程状态 0(课程已完成)或者1(课程未完成)
        int course_state=reqPage.getCourse_state();
        //设置分页信息
        PageHelper.startPage(reqPage.getCurrentPage(),reqPage.getPageSize());
        //根据老师id和状态获取 课程列表
        List<McTeacherCourseDto> courseList= mcCourseDao.findMcTeacherCourseList(tea_id,course_state);
        //查询记录总页数
        Integer courseCount=mcCourseDao.findMcTeacherCourseCount(tea_id,course_state);
        reqPage.setTotalRecord(courseCount);
        return courseList;
    }

    /**
     * 查询 老师--课程中的课列表
     * @param reqPage
     * @return
     */
    public Page<McTeaScheduleDto> getMcScheduleList(RequestPage reqPage) {
        //设置分页信息
        Page page=PageHelper.startPage(reqPage.getCurrentPage(),reqPage.getPageSize());
        //根据课程id 查询课列表
        mcCourseScheduleDao.findScheduleAndTeaList(reqPage.getMcCourseId());
        return page;
    }
}
